package com.qm.console.config;

import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;

import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {

    private final String pathMapping="/console/api";

    private ApiInfo initApiInfo() {
        ApiInfo apiInfo = new ApiInfo("[ VChat Console 接口文档 ]",//大标题
                initContextInfo(),//简单的描述
                "1.0.0",//版本
                "服务条款",
                "[ xxzhang ]",//作者
                "测试服务器入口>>",
                "http://47.111.116.56/"
        );

        return apiInfo;
    }

    private String initContextInfo() {
        StringBuffer sb = new StringBuffer();
        sb.append("[ 花伴控制台 接口文档 ]")
                .append("<br/>");

        return sb.toString();
    }


    @Bean
    public Docket restfulApi() {
        System.out.println("springfox.documentation.swagger.v2.host " + pathMapping + "/swagger-ui.html");

         return new Docket(DocumentationType.SWAGGER_2)
                .groupName("RestfulApi")
                .genericModelSubstitutes(DeferredResult.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
//                .pathMapping(pathMapping) // base，最终调用接口后会和paths拼接在一起
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.qm.console.controller.rest"))
                .build()
                .apiInfo(initApiInfo());
    }
}
